package com.demo.controller;


import cn.hutool.core.util.StrUtil;
import com.basic.domain.ApiResult;
import com.basic.domain.PageResult;
import com.demo.service.IProductService;
import com.demo.service.criteria.ProductQueryCriteria;
import com.demo.service.dto.ProductDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-12
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @PostMapping("/getProductByPage")
    public ApiResult<PageResult<ProductDto>> getProductByPage(@RequestBody ProductQueryCriteria criteria, Pageable pageable) {
        return ApiResult.ok(productService.queryAll(criteria, pageable));
    }

    @GetMapping("/getProductById")
    public ApiResult<ProductDto> getProductById(@RequestParam Long id) {
        return ApiResult.ok(productService.queryById(id));
    }

    @PostMapping("getProductOne")
    public ApiResult<ProductDto> getProductOne(@RequestBody ProductQueryCriteria criteria) {
        return ApiResult.ok(productService.queryOne(criteria));
    }

    @PostMapping("/saveOrUpdateProduct")
    public ApiResult<ProductDto> saveOrUpdateProduct(@RequestBody ProductDto ProductDto) {
        return ApiResult.ok(productService.saveOrUpdate(ProductDto));
    }

    @GetMapping("/deleteProductById")
    public ApiResult<Object> deleteProductById(@RequestParam Long id) {
        return ApiResult.ok(StrUtil.format("成功删除{}条数据", productService.deleteId(id)));
    }

    @PostMapping("/deleteProductMore")
    public ApiResult<Object> deleteProductMore(@RequestBody ProductQueryCriteria criteria) {
        productService.deleteByMore(criteria);
        return ApiResult.ok("删除成功");
    }
}
